(ns org.dawnsql.tools.my-nosql
    (:require
        [org.dawnsql.tools.my-lexical :as my-lexical]
        [clojure.core.reducers :as r]
        [clojure.string :as str])
    (:import (com.google.common.base Strings)
             (cn.nosql.tools MyConvertUtil MyDefindCache)
             (java.util List ArrayList Hashtable Date Iterator))
    (:gen-class
        :implements [cn.nosql.service.INoSql]
        ; 生成 class 的类名f
        :name cn.nosql.service.MyNoSql
        ; 是否生成 class 的 main 方法
        :main false
        ))

; 定义数据库
(defn defind-db [lst]
    (if-let [ht (my-lexical/get-config-lst lst)]
        (if (and (some? (MyDefindCache/definDb ht)) (.containsKey ht "name"))
            (MyDefindCache/addDb (.get ht "name") ht))))

; 转换为 ArrayList
(defn to-list
    ([lst] (to-list lst (ArrayList.)))
    ([[f & r] lst]
     (if (some? f)
         (recur r (doto lst (.add f)))
         lst)))

; 添加数据
; set cache_name my-key my-value
(defn set-data [cache_name my-key my-value]
    (if-let [store (MyDefindCache/getStore cache_name)]
        (let [m (.openMap store (format "%s_cache" cache_name))]
            (.put m (MyConvertUtil/toObj my-key) (MyConvertUtil/toObj my-value))
            (.commit store)
            (.close store))))

; 添加数据
; set cache_name my-key my-value
(defn set-data-lst [cache_name [f & r]]
    (if-let [store (MyDefindCache/getStore cache_name)]
        (let [m (.openMap store (format "%s_cache" cache_name)) sb (StringBuilder.)]
            (try
                (do
                    (.put m f (first r))
                    (.commit store)
                    (.append sb "添加数据成功！"))
                (catch Exception ex
                    (.append sb (.getMessage ex)))
                (finally (.close store)))
            (.toString sb))))

; 添加数据
; mset cache_name my-key my-value [...]
(defn mset-data [cache_name lst]
    (if-let [store (MyDefindCache/getStore cache_name)]
        (let [m (.openMap store (format "%s_cache" cache_name))]
            (loop [[f & r] lst]
                (if (some? f)
                    (do
                        (.put m f (first r))
                        (recur (rest r)))))
            (.commit store)
            (.close store))))

; 获取数据
; get cache_name my-key
(defn get-data [cache_name my-key]
    (if-let [store (MyDefindCache/getStore cache_name)]
        (let [m (.openMap store (format "%s_cache" cache_name))]
            (.get m (MyConvertUtil/toObj my-key))
            (.close store))))

; 执行方法
(defn execute-nosql [^String sql]
    (if (some? sql)
        (let [lst (my-lexical/to-back sql)]
            (let [f (first lst) l (last lst)]
                (cond (and (= f "{") (= l "}")) (if (nil? (defind-db lst))
                                                    "执行成功！" "执行失败！")
                      ;(and (= f "insert") (= (count lst) 4)) (insert-data ())
                      )))))

; 实现接口的方法
(defn -executeSqlQuery [this ^String sql]
    (execute-nosql sql))

; 实现接口的方法
(defn -insertNoSql [this ^String cache_name ^String method_name ^List my-key, ^List my-value]
    (if (some? method_name)
        (cond (= (str/lower-case method_name) "set") (if (nil? (MyDefindCache/setData cache_name my-key my-value))
                                                         (MyConvertUtil/objToListBytes "保存成功！"))
              )))

; 实现接口的方法
(defn -executeNoSql [this ^String cache_name ^String method_name ^List my-key]
    (if (some? method_name)
        (cond (= (str/lower-case method_name) "mset") (if (nil? (MyDefindCache/msetData cache_name (to-list (MyConvertUtil/restoreToObject my-key))))
                                                          (MyConvertUtil/objToListBytes "保存成功！"))
              (= (str/lower-case method_name) "get") (MyConvertUtil/objToListBytes (MyDefindCache/getData cache_name (MyConvertUtil/restoreToObject my-key)))      ;(get-data cache_name my-key)
              (= (str/lower-case method_name) "set") (do
                                                         (println (to-list (MyConvertUtil/restoreToObject my-key)))
                                                         (if (nil? (MyDefindCache/msetData cache_name (to-list (MyConvertUtil/restoreToObject my-key))))
                                                             (MyConvertUtil/objToListBytes "保存成功！")))
            )))

















































